package com.framework.controller.newcontroller;

import com.framework.entity.sys.SysShare;
import com.framework.entity.tran.AliPayerAgent;
import com.framework.exception.util.RespResult;
import com.framework.service.AgentInfoService;
import com.framework.service.AgentService;
import com.framework.service.AliPayerAgentService;
import com.framework.service.TreeAgentService;
import com.framework.service.agent.SysShareService;
import com.framework.view.AgentVerifiedForm;
import com.framework.view.AgentView;
import com.framework.view.NewAgentVerifiedForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 合伙人控制器
 *
 * @author wq
 */
@RestController
@RequestMapping("new/sys")
@Api(value = "合伙人模块", tags = {"合伙人模块"})
public class AgentNewController {

    @Autowired
    private SysShareService sysShareService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private AliPayerAgentService aliPayerAgentService;

    @Autowired
    private TreeAgentService treeAgentService;


    @GetMapping("user/getShareList")
    @ApiOperation("获取文案页面信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true)

    })
    public RespResult getShareList(String token, Integer pageNo) {
        List<SysShare> list = sysShareService.getShareList(token, pageNo);
        return RespResult.ok().put("list", list);
    }

    /**
     * 根据token获取人物信息
     * @param token
     * @return
     */
    @GetMapping("user/getAgentByToken")
    @ApiOperation("根据token获取人物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),

    })
    public RespResult getInfoByToken(String token) {
        Map<String, Object> map = agentService.getInfoByToken(token);
        return RespResult.ok(map);
    }

    /**
     * 修改身份证号
     * @param token
     * @param identity
     * @param smsCode
     * @return
     */
    @PostMapping("user/updateIdentity")
    @ApiOperation("修改身份证号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "identity", value = "身份证号码", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "smsCode", value = "验证码", dataType = "string", paramType = "query", required = true)

    })
    public RespResult updateIdentity(String token,String identity,String smsCode) {
        agentService.updateIdentity(token,identity,smsCode);
        return RespResult.ok();
    }

    /**
     * 修改银行卡
     * @param token
     * @param bankName
     * @param smsCode
     * @param bankCode
     * @return
     */
    @PostMapping("user/updateBank")
    @ApiOperation("修改银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "bankName", value = "银行卡名称", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "smsCode", value = "验证码", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "bankCode", value = "银行卡号码", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "预留手机号", dataType = "string", paramType = "query")
    })
    public RespResult updateBank(String token,String bankName,String smsCode,String bankCode,String phone) {
        agentService.updateBank(token,bankName,smsCode,bankCode,phone);
        return RespResult.ok();
    }

    /**
     * 修改账号
     * @param token
     * @param newPhone
     * @param smsCode
     * @return
     */
    @PostMapping("user/updatePhone")
    @ApiOperation("修改手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "newPhone", value = "银行卡名称", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "smsCode", value = "验证码", dataType = "string", paramType = "query", required = true),

    })
    public RespResult updatePhone(String token,String newPhone,String smsCode) {
        agentService.updatePhone(token,newPhone,smsCode);
        return RespResult.ok();
    }

    /**
     * 修改姓名
     * @param token
     * @param newName
     * @param smsCode
     * @return
     */
    @PostMapping("user/updateName")
    @ApiOperation("修改姓名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录产生的token", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "newName", value = "姓名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "smsCode", value = "验证码", dataType = "string", paramType = "query", required = true),

    })
    public RespResult updateName(String token,String newName,String smsCode) {
        agentService.updateName(token,newName,smsCode);
        return RespResult.ok();
    }

    /**
     * 进入提现页面
     * @param token 当前登录人token
     */
    @GetMapping("user/withdrawInfo")
    @ApiOperation("进入提现页面")
    public RespResult withdrawInfo(String token) {
        Map<String,Object> map = agentService.takeIndexInfo(token);
        return RespResult.ok().put("data",map);
    }

    /**
     * 提现
     * @param password 密码
     * @param money  提现金额
     * @param token 登录token
     * @param bankNo 银行卡号
     */
    @PostMapping("user/takeMoney")
    @ApiOperation("提现")
    @ApiImplicitParams({
            @ApiImplicitParam(name="password",value="提现密码 aes加密",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="money",value="提现金额",dataType="BigDecimal", paramType = "query",required = true),
            @ApiImplicitParam(name="bankNo",value="银行卡号",dataType="string", paramType = "query",required = false),
    })
    public RespResult takeMoney(String token, String password, BigDecimal money, String bankNo) {
        agentInfoService.takeAmount(token,password,money,bankNo);
        return RespResult.ok();
    }

    /**
     * 实名认证
     * @param form 表单
     * @param token 登录token
     */
    @PostMapping("user/agentVerified")
    @ApiOperation("实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name="vCode",value="手机验证码",dataType="string", paramType = "query",required = true)
    })
    public RespResult agentVerified(NewAgentVerifiedForm form, String token) {
        agentService.verifiedAgent(form,token);
        return RespResult.ok();
    }


    /**
     * 根据token 获取支付宝信息
     * @param token
     * @return
     */
    @GetMapping("user/queryAliAccount")
    @ApiOperation("查询个人的支付宝信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true)
    })
    public RespResult queryAliAccount(String token){
        AliPayerAgent aliPayerAgent=aliPayerAgentService.queryAliAccount(token);
        return RespResult.ok().put("aliAccount",aliPayerAgent);
    }

    /**
     * 绑定支付宝账号
     * @return
     */
    @PostMapping("user/bindAliAccount")
    @ApiOperation("绑定支付宝账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true),
            @ApiImplicitParam(name="name",value="支付宝姓名",dataType ="string",paramType = "query",required = true),
            @ApiImplicitParam(name="aliAccount",value="支付宝账号",dataType ="string",paramType = "query",required = true)
    })
    public RespResult bindAliAccount(String token,String name,String aliAccount){
        aliPayerAgentService.bindAliAccount(token,name,aliAccount);
        return RespResult.ok();
    }

    /**
     * 修改支付宝账号
     * @return
     */
    @PostMapping("user/updateAliAccount")
    @ApiOperation("修改支付宝账号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true),
            @ApiImplicitParam(name="name",value="支付宝姓名",dataType ="string",paramType = "query",required = true),
            @ApiImplicitParam(name="aliAccount",value="支付宝账号",dataType ="string",paramType = "query",required = true)
    })
    public RespResult updateAliAccount(String token,String name,String aliAccount){
        aliPayerAgentService.updateAliAccount(token,name,aliAccount);
        return RespResult.ok().put("msg","修改成功");
    }

    /**
     * 找回登录密码
     * @param phone 手机号码
     * @param password 密码
     * @param confPassword 确认密码
     * @param smsCode 短信验证码
     */
    @PostMapping("loginForgetPassword")
    @ApiOperation("找回登录密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="手机号码",dataType="string", paramType = "query" ,required = true),
            @ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name = "deptId",value="渠道id",dataType ="Long",paramType = "query",required = false)
    })
    public RespResult forgetPassword(String phone,String password,String confPassword,String smsCode,Long deptId) {
        agentService.forgetPassword(phone,password,confPassword,smsCode,deptId);
        return RespResult.ok().put("msg","密码找回成功");
    }


    /**
     * 找回交易密码
     * @param password 密码
     * @param confPassword 确认密码
     * @param token token
     * @param smsCode 手机验证码
     */
    @PostMapping("user/payForgetPassword")
    @ApiOperation("找回交易密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="token",value="验证码",dataType="string", paramType = "query",required = true),
    })
    public RespResult payForget(String password,String confPassword,String token,String smsCode) {
        agentInfoService.updateTaskPassword(password,confPassword,token,smsCode);
        return RespResult.ok().put("msg","密码找回成功");
    }

    /**
     * 修改登录密码
     * @param token
     * @param password
     * @param confPassword
     * @param smsCode
     * @param oldPassword
     * @return
     */
    @PostMapping("user/updatePassword")
    @ApiOperation("修改登录密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name = "oldPassword",value="渠道id",dataType ="Long",paramType = "query",required = true),
            @ApiImplicitParam(name = "token",value="登录产生的token",dataType ="Long",paramType = "query",required = true)
    })
    public RespResult updatePassword(String token,String password,String confPassword,String smsCode,String oldPassword) {
        agentService.updatePassword(token,password,confPassword,smsCode,oldPassword);
        return RespResult.ok().put("msg","密码修改成功");
    }

    /**
     * 修改交易密码
     * @param token
     * @param password
     * @param confPassword
     * @param smsCode
     * @param oldPassword
     * @return
     */
    @PostMapping("user/updateTradePassword")
    @ApiOperation("修改交易密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name = "oldPassword",value="旧密码",dataType ="Long",paramType = "query",required = true),
            @ApiImplicitParam(name = "token",value="登录产生的token",dataType ="Long",paramType = "query",required = true)
    })
    public RespResult updateTradePassword(String token,String password,String confPassword,String smsCode,String oldPassword) {
        agentService.updateTradePassword(token,password,confPassword,smsCode,oldPassword);
        return RespResult.ok().put("msg","密码修改成功");
    }

    /**
     * 功能描述: 注册
     * @param phone 手机号码
     * @param password 密码
     * @param confPassword 确认密码
     * @param smsCode 注册短信
     * @param pid  推荐码
     * @param name 昵称
     */
    @PostMapping("register")
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="手机号码",dataType="string", paramType = "query" ,required = true),
            @ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="pid",value="推荐码",dataType="Long", paramType = "query",required = true),
            @ApiImplicitParam(name="name",value="真实姓名",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="deptId",value="渠道id",dataType="Long", paramType = "query",required = true)
    })
    public RespResult register(String phone,String password,String confPassword,String smsCode,Long pid,String name,Long deptId) {
        agentService.register(phone,password,confPassword,smsCode,pid,name,deptId);
        return RespResult.ok();
    }

    /**
     *获取钱包余额 页面信息
     * @param token
     * @return
     */
    @PostMapping("user/getWalletInfo")
    @ApiOperation("获取钱包余额页面信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query" ,required = true)
    })
    public RespResult getWalletInfo(String token) {
        Map<String,Object> map=agentService.getWalletInfo(token);
        return RespResult.ok().put("data",map);
    }


    /**
     * 修改头像
     * @param token
     * @param img 图片地址
     * @return
     */
    @PostMapping("user/updateHeadImg")
    @ApiOperation("修改头像图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name="img",value="头像img图片地址",dataType="string", paramType = "query",required = true)
    })
    public RespResult updateHeadImg(String token,String img) {
        agentService.updateHeadImg(token,img);
        return RespResult.ok();
    }


    /**
     * 生成二维码
     * @param token
     * @return
     */
    @PostMapping("user/generateQR")
    @ApiOperation("生成二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query",required = true)
    })
    public RespResult generateQR(String token) {
        agentService.generateQR(token);
        return RespResult.ok();
    }

    /**
     * 生成二维码
     * @param token
     * @return
     */
    @PostMapping("user/shareByToken")
    @ApiOperation("分享二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="shareId",value="所分享的id",dataType="string", paramType = "query",required = true),
    })
    public RespResult shareByToken(String token,Integer shareId) {
        List<String> list=agentService.shareByToken(token,shareId);
        return RespResult.ok().put("list",list);
    }


    /**
     * 获取个人客服
     * @param token
     * @return
     */
    @PostMapping("user/getCusService")
    @ApiOperation("获取个人客服")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query",required = true)
    })
    public RespResult getCusService(String token) {
        Map<String,Object> map=agentService.getCusService(token);
        return RespResult.ok().put("data",map);
    }


    /**
     * 设置是否接受消息
     * @param token
     * @return
     */
    @PostMapping("user/setPushState")
    @ApiOperation("设置是否接受消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="state",value="推送状态 0:不推送 1：推送",dataType="integer", paramType = "query",required = true)

    })
    public RespResult setPushState(String token,Integer state) {
        agentService.setPushState(token,state);
        return RespResult.ok();
    }

    /**
     * 查询下级代理商列表信息
     * @param token token
     * @return
     */
    @GetMapping("user/querySubAgentList")
    @ApiOperation("查询下级代理商列表信息")
    public RespResult querySubAgentList(String token) {
        List<AgentView> list = treeAgentService.querySubAgent(token);
        return RespResult.ok().put("list",list);
    }

    /**
     * 分享后清理多余图片
     * @param token token
     * @return
     */
    @PostMapping("user/cleanImg")
    @ApiOperation("分享后清理多余图片")
    public void cleanImg(String token, String[] imgUrls) {
        agentService.cleanImg(token,imgUrls);
    }


    /**
     * 合伙人列表明细
     * @param token
     * @param partnerCount
     * @param thisMonthAmount
     * @param merchantCount
     * @return
     */
    @GetMapping("user/queryPartnerMap")
    @ApiOperation("合伙人列表明细 团队统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name="partnerCount",value="合伙人数量（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="thisMonthAmount",value="本月交易总额（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="merchantCount",value="商户数量（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="realName",value="是否实名 (1 0 slient)",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="name",value="姓名",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="thisMonthActiv",value="当月激活 desc,不选择激活则不传",dataType="string", paramType = "query",required = false)
    })
    public RespResult queryPartnerMap(String token,String partnerCount,String thisMonthAmount,String merchantCount,String realName,String name,String thisMonthActiv) {
        Map<String,Object> map = agentService.queryPartnerInfo(token,partnerCount,thisMonthAmount,merchantCount,realName,name,thisMonthActiv);
        return RespResult.ok().put("data",map);
    }
    /**
     * 合伙人详情
     * @param token
     * @param types
     * @param time
     * @return
     */
    @GetMapping("user/queryAgentInfo")
    @ApiOperation("合伙人详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="types",value="日维度(day) 月维度(month)",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="time",value="时间维度 月(yyyy-mm) 日(yyyy-mm-dd)",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="agentId",value="查询的代理商id",dataType="int", paramType = "query",required = true)
    })
    public RespResult queryAgentInfo(String token,String types,String time,Long agentId) {
        Map<String,Object> map = agentService.queryAgentInfo(token,types,time,agentId);
        return RespResult.ok().put("data",map);
    }

    /**
     * 完善信息(优付代付签约)
     * @param token
     * @param phone
     * @param identityHead
     * @param identityEm
     * @param vCode
     * @return
     */
    @ApiOperation("完善信息(优付代付签约)")
    @PostMapping("user/signContract")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="登陆产生的token",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="identityHead",value="头像面",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="identityEm",value="国徽面",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="phone",value="预留手机号码",dataType="string", paramType = "query",required = true),
            @ApiImplicitParam(name="vCode",value="验证码",dataType="string", paramType = "query",required = true)
    })
    public RespResult signContract(String token, String  phone, String identityHead, String identityEm,String vCode){
        agentService.signContractByToken(token,phone,identityHead,identityEm,vCode);
        return RespResult.ok();
    }
}
